//
//  IServerCommunicator.swift
//  APPDomain
//
//  Created by raymond Peng on 2018/8/15.
//  Copyright © 2018 TanBaYe. All rights reserved.
//

import Foundation

public typealias CommunicationCompletion = (ServerResponse) -> Void

public protocol IServerCommunicator {
    /// 使用 URLSession 进行网络请求
    ///
    /// - Parameters:
    ///   - request: 请求对象
    ///   - completionHandler: 完成块
    func requestWith(request: IDataRequest, completionHandler: @escaping CommunicationCompletion)

    /// 设置每次请求都会包含的 header (比如一些认证参数等)
    ///
    /// - Parameter headers: header
    func setAddtionalHeaders(_ headers: [String: String])
}

/// 服务器响应的表示
public enum ServerResponse {
    case success(data: Data)
    case failed(data: Data?, error: DataError?)
}

/// HTTP 方法
///
/// - get: get
/// - post: post
/// - delete: delete
/// - update: update
public enum HTTPRequestMethod: String {
    case GET
    case POST
    case DELETE
    case PUT
}

/// HTTP 请求参数编码类型
///
/// - queryString: 在请求 URL 中编码
/// - httpBody: 在请求体中编码
public enum RequestParameterEncoding {
    case queryString
    case httpBody
}

/// 数据请求实体封装
public protocol IDataRequest {
    var url: URL { get }
    var method: HTTPRequestMethod { get }
    var params: [String: Any]? { get }
    var headers: [String: String]? { get }
    var encoding: RequestParameterEncoding { get }
    var requestBody: Data? { get }
}
